<?php

class Check
{
    public static $error_info = false;

    public static function AdminToken($de_token): array
    {

        $token = Rc4::decode($de_token, Tool::ini('Rc4Key'), true);
        if (!$token) return self::ini(10005);
        if (!json_decode($token, true)) return self::ini(10005);
        $token = json_decode($token, true);
        $db = Db::get();
        $sql = 'select * from admin_login_log where token = ? and create_ip = ?;';
        $token_info = $db->getRow($sql, [$token['token'], $token['ip']]);
        if (!$token_info) return self::ini(10006);
        if (time() - intval($token['time']) >= intval(Tool::ini('AdminTokenTime', 864000))) return self::ini(10007);
        return ['check' => true, 'data' => $token];
    }

    public static function AdminInfo($admin_info): array
    {
        if (!$admin_info) return self::ini(10003);
        if ($admin_info['status'] == '0') return self::ini(10004);
        return ['check' => true];
    }

    public static function Input($obj, $empty = true): array
    {
        foreach ($obj as $key => $item) {
            if ($item === false) return self::ini(10002, $key);
            if ($item === '' && $empty) return self::ini(10002, $key);
        }
        return ['check' => true];
    }

    public static function DecodeInput($input): array
    {
        if (!json_decode($input, true)) return self::ini(10001);
        return ['check' => true];
    }

    // region 读取 error ini
    public static function ini($key, $message = '发生错误'): array
    {
        if (!$key) return ['check' => false, 'code' => 201, 'message' => $message];
        if (!self::$error_info) {
            $path = dirname(__DIR__) . '/error.ini';
            $config_file_path = $path;
            self::$error_info = parse_ini_file($config_file_path);
        }
        $config_info = self::$error_info;
        if (isset($config_info['ERROR_' . $key]) && $message == '发生错误') $message = $config_info['ERROR_' . $key];
        return ['check' => false, 'code' => $key, 'message' => $message];
    }
    // endregion
}